//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Research.ScientificWorkflow.TridentModelSL;
using Microsoft.Research.ScientificWorkflow.WebClient.Common;
using Microsoft.Research.eResearch;
using Microsoft.Research.eResearch.Common.Linq;
using System.Windows.Browser;
using System.IO;
using System.Xml.Serialization;

namespace Microsoft.Research.ScientificWorkflow.WebClient.Views.WorkflowApplication
{
    /// <summary>
    /// Represents Navigation View in the trident work flow application
    /// </summary>
    public partial class NavigationView : UserControl, IView
    {
        #region Private fields

        /// <summary>
        /// 
        /// </summary>
        TridentApplication applicationInstance;

        /// <summary>
        /// 
        /// </summary>
        static ObservableCollection<BaseModel> leftPaneItems = new ObservableCollection<BaseModel>();

        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationView"/> class.
        /// </summary>
        public NavigationView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes the <see cref="NavigationView"/> class.
        /// </summary>
        static NavigationView()
        {
            RegistryService.GetWorkflowCategoriesCompleted += new EventHandler<GetWorkflowCategoriesCompletedEventArgs>(OnGetWorkflowCategoriesCompleted);
            RegistryService.GetMyWorkflowsForUserCompleted += new EventHandler<GetMyWorkflowsForUserCompletedEventArgs>(OnGetMyWorkflowsForUserCompleted);
            RegistryService.GetAllUsersMyWorkflowsCompleted += new EventHandler<GetAllUsersMyWorkflowsCompletedEventArgs>(OnGetAllUsersMyWorkflowsCompleted);
            RegistryService.GetAllSharedWorkflowsForCurrentUserCompleted += new EventHandler<GetAllSharedWorkflowsForCurrentUserCompletedEventArgs>(OnGetAllSharedWorkflowsForCurrentUserCompleted);
        }
        #endregion

        #region IView Members

        /// <summary>
        /// Fired for launching of the wait screen.
        /// </summary>
        public event EventHandler LaunchWaitScreen;

        /// <summary>
        /// Fired for removing of the wait screen.
        /// </summary>
        public event EventHandler RemoveWaitScreen;

        /// <summary>
        /// Fired for launching of the wait screen.
        /// </summary>
        private static event EventHandler LaunchWaitScreenStatic;

        /// <summary>
        /// Fired for removing of the wait screen.
        /// </summary>
        private static event EventHandler RemoveWaitScreenStatic;

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        public FrameworkElement View
        {
            get
            {
                return this;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Initialize()
        {
            this.applicationInstance = TridentApplication.Instance;

            leftPaneItems = new ObservableCollection<BaseModel>();
            this.treeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(OnSelectedItemChanged);
            this.treeView.ItemsSource = leftPaneItems;
            NavigationView.LaunchWaitScreenStatic = this.LaunchWaitScreen;
            NavigationView.RemoveWaitScreenStatic = this.RemoveWaitScreen;

            NavigationView.LaunchWait();
            RegistryService.GetWorkflowCategoriesAsync(TridentApplication.Instance.User);
        }

        #endregion

        #region Private methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            BaseModel selectedModel = ((BaseModel)e.NewValue);
            if (selectedModel is WorkflowModel)
            {
                if (JobCommands.WorkflowSelectedCommand != null)
                {
                    JobCommands.WorkflowSelectedCommand.Execute(e.NewValue);
                }
            }
        }

        private static void LaunchWait()
        {
            if (NavigationView.LaunchWaitScreenStatic != null)
            {
                NavigationView.LaunchWaitScreenStatic.Invoke(TridentApplication.Instance, new EventArgs());
            }
        }

        private static void RemoveWait()
        {
            if (NavigationView.RemoveWaitScreenStatic != null)
            {
                NavigationView.RemoveWaitScreenStatic.Invoke(TridentApplication.Instance, new EventArgs());
            }
        }

        /// <summary>
        /// Called when [get workflow categories completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Research.ScientificWorkflow.GetWorkflowCategoriesCompletedEventArgs"/> instance containing the event data.</param>
        private static void OnGetWorkflowCategoriesCompleted(object sender, GetWorkflowCategoriesCompletedEventArgs e)
        {
            if (e.Error == null && e.Categories != null)
            {
                e.Categories.ForEach(child => leftPaneItems.Add(child));
            }

            RegistryService.GetMyWorkflowsForUserAsync(TridentApplication.Instance.User);
        }

        /// <summary>
        /// Called when [get my workflows for user completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Research.ScientificWorkflow.GetMyWorkflowsForUserCompletedEventArgs"/> instance containing the event data.</param>
        private static void OnGetMyWorkflowsForUserCompleted(object sender, GetMyWorkflowsForUserCompletedEventArgs e)
        {
            if (e.Error == null && e.Categories != null)
            {
                e.Categories.ForEach(child => leftPaneItems.Add(child));
            }

            if (TridentApplication.Instance.UserRole == UserRole.Admin)
            {
                RegistryService.GetAllUsersMyWorkflowsAsync(TridentApplication.Instance.User);
            }
            else
            {
                RegistryService.GetAllSharedWorkflowsForCurrentUserAsync(TridentApplication.Instance.User);
            }
        }

        /// <summary>
        /// Called when [get all users my workflows completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Research.ScientificWorkflow.GetAllUsersMyWorkflowsCompletedEventArgs"/> instance containing the event data.</param>
        private static void OnGetAllUsersMyWorkflowsCompleted(object sender, GetAllUsersMyWorkflowsCompletedEventArgs e)
        {
            try
            {

                if (e.Error == null && e.Categories != null)
                {
                    e.Categories.ForEach(child => leftPaneItems.Add(child));
                }
            }
            finally
            {
                NavigationView.RemoveWait();
            }

        }

        /// <summary>
        /// Called when [get all shared workflows for current user completed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.Research.ScientificWorkflow.GetAllSharedWorkflowsForCurrentUserCompletedEventArgs"/> instance containing the event data.</param>
        private static void OnGetAllSharedWorkflowsForCurrentUserCompleted(object sender, GetAllSharedWorkflowsForCurrentUserCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null && e.Categories != null)
                {
                    e.Categories.ForEach(child => leftPaneItems.Add(child));
                }
            }
            finally
            {
                NavigationView.RemoveWait();
            }
        }
        #endregion
    }
}
